#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>

static const char *server_addr = "192.168.0.246";
static const uint16_t server_port = 8000;

int main()
{
  int clientfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if ( clientfd == -1 )
    {
      perror("create socket failed.\n");
    }

    struct sockaddr_in serverAddr;
    bzero(&serverAddr,sizeof(serverAddr) );
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(server_port);
    int result = 0;
    result = inet_aton(server_addr, &(serverAddr.sin_addr) );
    if( result == -1 )
    {
        perror("inet_aton failed.\n");
        return -1;
    }
    result = connect(clientfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr) );
    if( result == -1 )
    {
        perror("connect failed.\n");
        return -1;
    }

    char szBuf[100] = {"I am testing the internet off."};
    //断网后，可以通过发送0个字节测试下是否能够检测到网络断开
    ssize_t nSize = send(clientfd, szBuf, 100, 0);
    sleep(10);
    //断开网线后
    nSize = recv(clientfd, szBuf, 100, MSG_PEEK);
    if( nSize == -1 )
    {
        perror("recv failed.\n");
        return -1;
    }

    getchar();
    return 0;
}
